package com.neusoft.eenie.signdemo.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.neusoft.eenie.signdemo.R;
import com.neusoft.eenie.signdemo.bean.FlagInfo;
import com.neusoft.eenie.signdemo.bean.UserInfo;
import com.neusoft.eenie.signdemo.http.HttpUtiles;
import com.neusoft.eenie.signdemo.http.OKHttpUtiles;
import com.neusoft.eenie.signdemo.utile.HtmlTools;
import com.orhanobut.logger.Logger;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

import java.io.IOException;

public class MainActivity extends BaseActivity implements View.OnClickListener {
    final String TAG = "MainActivity";

    EditText editName;
    EditText editPwd;


    Button btnSign;
    Button btnLoad;
    Button btnIndex;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        initView();
    }

    private void initView() {

        editName = (EditText) findViewById(R.id.editName);
        editPwd = (EditText) findViewById(R.id.editPwd);


        btnSign = (Button) findViewById(R.id.btnSign);
        btnSign.setOnClickListener(this);


        btnLoad = (Button) findViewById(R.id.btnLoad);
        btnLoad.setOnClickListener(this);

        btnIndex = (Button) findViewById(R.id.btnIndex);
        btnIndex.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnSign:
                UserInfo info = new UserInfo();
                info.setName(editName.getText().toString());
                info.setPwd(editPwd.getText().toString());
                signIn(info);
                break;


            case R.id.btnIndex:
                RequestBody body1 = RequestBody.create(OKHttpUtiles.formReq, "dmdate=2016-03-25&ck%5B%5D=11750&ck%5B%5D=12252&ck%5B%5D=12253&ck%5B%5D=12254&ck%5B%5D=12255&mid=&check_info=test");
                OKHttpUtiles.getInstance().post(HttpUtiles.URL_COMMIT, body1, new com.squareup.okhttp.Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        Logger.e(TAG, "request http fail");
                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                        Logger.e(TAG, "request http success");
                    }
                });

                break;
        }
    }


    public void signIn(final UserInfo info) {

//        showDialog("正在加载session...");
//
//        OKHttpUtiles.getInstance().get(HttpUtiles.URL_LOAD_SESSION, new com.squareup.okhttp.Callback() {
//            @Override
//            public void onFailure(Request request, IOException e) {
//
//                btnSign.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        showDialog("加载session失败！", true);
//                    }
//                });
//
//            }
//
//            @Override
//            public void onResponse(Response response) throws IOException {
//
//
//            }
//        });


        showDialog("正在登录...");

        RequestBody body = RequestBody.create(OKHttpUtiles.formReq, "username=" + info.getName() + "&password=" + info.getPwd());
        OKHttpUtiles.getInstance().post(HttpUtiles.URL_SIGN_IN, body, new com.squareup.okhttp.Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

                btnSign.post(new Runnable() {
                    @Override
                    public void run() {
                        showDialog("登录失败！", true);
                    }
                });
            }

            @Override
            public void onResponse(Response response) throws IOException {


                final FlagInfo info = HtmlTools.parseHtmlTitle(response.body().string());

                if (info.getFlag().equals("控制台")) {
                    Logger.e(TAG, "ID->" + info.getMid() + "\nmanager name: " + info.getManaName());


                    btnSign.post(new Runnable() {
                        @Override
                        public void run() {
                            missDialog();
                            Intent in = new Intent(MainActivity.this, SignActivity.class);
                            in.putExtra("mid", info.getMid());
                            in.putExtra("wel", info.getManaName());
                            startActivity(in);
                            finish();
                        }
                    });


                }
            }
        });
    }


}
